package com.walsai.pcsys.web.validator;

import org.springframework.validation.Errors;
import org.springframework.validation.Validator;

import com.walsai.pcsys.core.model.User;

public class UserValidator implements Validator {

	@Override
	public boolean supports(Class clazz) {
		return clazz.equals(User.class);
	}

	@Override
	public void validate(Object target, Errors errors) {
		User u = (User) target;
		if(u.getUsername().trim().length() < 6){
			errors.rejectValue("username", "error.invalid.username", "User name must be more than 6 characters.");
		}
		if(u.getPassword().length() < 8){
			errors.rejectValue("password", "error.password.username", "Password must be more than 8 chracters length.");
		}
		if(!u.getPassword().equals(u.getConfirmPassword())){
			errors.rejectValue("confirmPassword", "error.invalid.confirmPassword","Password confirmed is wrong. "+ u.getPassword() +" " + u.getConfirmPassword());
		}
		if(u.getPasswordHint().trim().length() < 1 || u.getPassword().equals(u.getPasswordHint())){
			errors.rejectValue("passwordHint", "error.invalid.passwordHint", "Invalid Password Hint");
		}
		if(u.getFirstName().trim().length() < 1){
			errors.rejectValue("firstName", "error.invalid.firstname", "Invalid First Name");
		}
		if(u.getLastName().trim().length() < 1){
			errors.rejectValue("lastName", "error.invalid.lastname", "Invalid Last Name");
		}
		if(u.getEmail().split("@").length != 2){
			errors.rejectValue("email", "error.invalid.email", "Invalid Email");
		}
	}

}
